﻿
Imports System.Threading
Imports osi.root.lock.slimlock
Imports osi.root.delegates
Imports spinlock = osi.root.lock.slimlock.spinlock

Public Class atomic_int(Of LOCK_T As {islimlock, Structure})
    Inherits atom(Of Int32, LOCK_T)

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal i As Int32)
        MyBase.New(i)
    End Sub

    Protected Overrides Function atomic_read(ByRef p As Int32) As Int32
        Return atomic.read(p)
    End Function

    Protected Overrides Sub atomic_write(ByRef p As Int32, ByVal i As Int32)
        atomic.eva(p, i)
    End Sub

    Public Function increment() As Int32
        Return Interlocked.Increment(p)
    End Function

    Public Function decrement() As Int32
        Return Interlocked.Decrement(p)
    End Function

    Public Function exchange(ByVal i As Int32) As Int32
        Return Interlocked.Exchange(p, i)
    End Function

    Public Function add(ByVal i As Int32) As Int32
        Return Interlocked.Add(p, i)
    End Function

    Public Overrides Function compare_exchange(ByVal value As Int32,
                                               ByVal comparand As Int32) As Int32
        Return Interlocked.CompareExchange(p, value, comparand)
    End Function
End Class

Public Class atomic_int
    Inherits atomic_int(Of spinlock)

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal i As Int32)
        MyBase.New(i)
    End Sub
End Class
